package experiment02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class jisuanqi extends JFrame {

    static ArrayList<String> list1 = new <String>ArrayList();
    static ArrayList<String> list2 = new <String>ArrayList();
    static int count = 0;

    public static void main(String[] args) {
        //主界面
        JFrame jFrame = new JFrame();
        //窗口名
        jFrame.setTitle("简单计算器");
        //关闭事件
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //窗口大小不能调
        jFrame.setResizable(false);
        //窗口大小
        jFrame.setSize(300, 180);
        //窗口大小
        jFrame.setLocation(300, 300);

        //布局管理器
        GridBagLayout gridBagLayout = new GridBagLayout();
        jFrame.setLayout(gridBagLayout);
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;


        JPanel jp1 = new JPanel();
        mianbanbuju(gridBagLayout, gridBagConstraints, jp1, 0, 0, 4, 1);

        JTextField jt1 = new JTextField(20);
        //文本内容不可编辑
        jt1.setEnabled(false);
        jp1.add(jt1);
        jFrame.add(jp1);
        JButton button1 = new JButton("CE");
        jp1.add(button1);


        JPanel jp2 = new JPanel(new GridLayout(4, 4));
        mianbanbuju(gridBagLayout, gridBagConstraints, jp2, 0, 1, 4, 4);

        JButton jb1 = new JButton("1");
        JButton jb2 = new JButton("2");
        JButton jb3 = new JButton("3");
        JButton jb4 = new JButton("4");
        JButton jb5 = new JButton("5");
        JButton jb6 = new JButton("6");
        JButton jb7 = new JButton("7");
        JButton jb8 = new JButton("8");
        JButton jb9 = new JButton("9");
        JButton jb10 = new JButton("0");
        JButton jb11 = new JButton(".");
        JButton jb12 = new JButton("+");
        JButton jb13 = new JButton("-");
        JButton jb14 = new JButton("*");
        JButton jb15 = new JButton("/");
        JButton jb16 = new JButton("=");
        qianjiaanniu(jp2, jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9, jb10, jb11, jb12, jb13, jb14, jb15, jb16);
        jFrame.add(jp2);

        /*为数字按钮和小数点按钮添加点击事件，点击之后按钮所代表数字显示在文本编辑框*/
        zifu(jt1, jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9, jb10, jb11);

        jisuanfuhao(jt1, jb12, jb13, jb14, jb15, jb16);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText("");
                list1.clear();
                list2.clear();
                count = 0;
            }
        });

        jFrame.setVisible(true);
    }

    private static void qianjiaanniu(
            JPanel jp2, JButton jb1, JButton jb2, JButton jb3, JButton jb4,
            JButton jb5, JButton jb6, JButton jb7, JButton jb8, JButton jb9,
            JButton jb10, JButton jb11, JButton jb12, JButton jb13, JButton jb14,
            JButton jb15, JButton jb16) {
        jp2.add(jb1);
        jp2.add(jb2);
        jp2.add(jb3);
        jp2.add(jb12);
        jp2.add(jb4);
        jp2.add(jb5);
        jp2.add(jb6);
        jp2.add(jb13);
        jp2.add(jb7);
        jp2.add(jb8);
        jp2.add(jb9);
        jp2.add(jb14);
        jp2.add(jb11);
        jp2.add(jb10);
        jp2.add(jb16);
        jp2.add(jb15);
    }

    private static void mianbanbuju(GridBagLayout gridBagLayout, GridBagConstraints gridBagConstraints,
                                    JPanel jp1, int a, int b, int c, int d) {
        gridBagConstraints.gridx = a;
        gridBagConstraints.gridy = b;
        gridBagConstraints.gridwidth = c;
        gridBagConstraints.gridheight = d;
        gridBagLayout.setConstraints(jp1, gridBagConstraints);
    }

    private static void jisuanfuhao(JTextField jt1, JButton jb12, JButton jb13, JButton jb14,
                                    JButton jb15, JButton jb16) {
        jb12.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.add(jt1.getText());
                list2.add("+");
                count++;
                jt1.setText("");
            }
        });
        jb13.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.add(jt1.getText());
                list2.add("-");
                count++;
                jt1.setText("");
            }
        });
        jb14.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.add(jt1.getText());
                list2.add("*");
                count++;
                jt1.setText("");
            }
        });
        jb15.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.add(jt1.getText());
                list2.add("/");
                count++;
                jt1.setText("");
            }
        });
        jb16.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.add(jt1.getText());
                jt1.setText(JISUAN(list2.get(count - 1)));
            }
        });
    }

    /*为数字按钮和小数点按钮添加点击事件，点击之后按钮所代表数字显示在文本编辑框*/
    private static void zifu(JTextField jt1, JButton jb1, JButton jb2, JButton jb3, JButton jb4,
                             JButton jb5, JButton jb6, JButton jb7, JButton jb8, JButton jb9,
                             JButton jb10, JButton jb11) {
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "1");
            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "2");
            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "3");
            }
        });
        jb4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "4");
            }
        });
        jb5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "5");
            }
        });
        jb6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "6");
            }
        });
        jb7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "7");
            }
        });
        jb8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "8");
            }
        });
        jb9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "9");
            }
        });
        jb10.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + "0");
            }
        });
        jb11.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jt1.setText(jt1.getText() + ".");
            }
        });
    }


    public static String JISUAN(String str) {
        Double c = 0.00;
        Double a = Double.parseDouble(list1.get(2 * count - 2));
        Double b = Double.parseDouble(list1.get(2 * count - 1));
        switch (str) {
            case "+" -> {
                c = a + b;
            }
            case "-" -> {
                c = a - b;
            }
            case "*" -> {
                c = a * b;
            }
            case "/" -> {
                c = a / b;
            }
            default -> {
            }
        }
        return String.valueOf(c);
    }

}
